package com.example.permission.common.result;


import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;


/**
 * 统一API响应结果封装
 */
@Data
public class Result<T> implements Serializable {

    @ApiModelProperty("返回码")
    private String code;
    @ApiModelProperty(value = "成功标记")
    private boolean success;
    @ApiModelProperty("返回消息")
    private String message;
    @ApiModelProperty("返回数据")
    private T data;

    private Result() {
    }

    private Result(String code, boolean success, String message) {
        this.code = code;
        this.success = success;
        this.message = message;
    }

    private Result(String code, boolean success, String message, T data) {
        this.data = data;
        this.code = code;
        this.message = message;
        this.success = success;
    }

    public static <T> Result<T> create() {
        return new Result<T>();
    }

    public static <T> Result<T> fail() {
        return new Result<>(ResultCode.FAIL.getCode(), false, ResultCode.FAIL.getMessage());
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<>(ResultCode.FAIL.getCode(), false, msg);
    }

    public static <T> Result<T> fail(String code, boolean success, String msg, T data) {
        return new Result<>(code, success, msg, data);
    }

    public static <T> Result<T> fail(String code, String msg) {
        return new Result<>(code, false, msg);
    }

    public static <T> Result<T> fail(String code, boolean success, String msg) {
        return new Result<>(code, success, msg);
    }

    public static <T> Result<T> success() {
        return new Result<>(ResultCode.SUCCESS.getCode(), true, ResultCode.SUCCESS.getMessage());
    }

    public static <T> Result<T> success(String msg) {
        return new Result<>(ResultCode.SUCCESS.getCode(), true, msg);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), true, ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), true, msg, data);
    }
}